<?php

namespace App\Http\Controllers\admin;

use App\model\Activity;
use App\model\UserModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Validator;

class RegisterController extends Controller
{
    public function register(Request $request)
    {
        /*$da = $request->all();
        dd($da);
        exit;*/
        if ($request->isMethod('POST')) {
            /*var_dump($request->all());
            exit;*/
            $this->validate($request, [
                'name' => 'required',
                'password' => 'required|string|min:6|max:20|confirmed',
                'password_confirmation' => 'required|string|between:4,20',
                'email' => 'exists:staff,email,account_id,1'
            ], [
                'required' => ':attribute为必填项',
                'int' => ':attribute为整型，请重新填写',
                'between' => ':attribute长度在4到20之间',
                'max' => ':attribute长度不符，请重新填写',
                'min' => ':attribute长度不符，请重新填写',
                'confirmed'=>':attribute与密码不符，请重新填'
            ], [
                'name' => '姓名',
                'password' => '用户密码',
                'password_confirmation' => '确认密码',
                'email' => '邮箱'
            ]);

            /*if ($validator->fails()) {
                return redirect('AdmibViews.register')->withErrors($validator)->withInput();
            } else {*/
                $date = new UserModel();
                $date->name = $request->name;
                $date->email = $request->email;
                $date->password = $request->password;
                $date->updated_at = $request->updated_at;
                $date->created_at = $request->created_at;

                if ($date->save()) {
                    return redirect('index')->with('message', '注册成功');
                } else {
                    return back()->with('message', '注册失败');
                }
            }

        return view('AdmibViews.register');
    }
}
